package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import entity.User;

import entity.Cart;
import entity.Goods;
import utils.JdbcUtils;

public class CartDao{
	
	// {1}增加
	public void addCart(Cart cart) throws SQLException {
		String sql = "insert into cart(uid,gid,shopnum)values(?,?,?)";// ?表示占位符，填充插入占位符时表中有几列就写几
		int row = JdbcUtils.update(sql, cart.getUid(),cart.getGid(),cart.getShopnum());
		System.out.println("已增加" + row + "行数据");
	}

	// {2}删除
	public void deleteCart(Cart cart) throws SQLException {
		String sql = "delete from cart where cartid=?";
		int row = JdbcUtils.update(sql, cart.getCartid());
		System.out.println("已删除" + row + "行数据");
	}

	// {3}修改
	public void updateCart(Cart cart) throws SQLException {
		String sql = "update cart set shopnum=? where uid=? and gid=?";
		int row = JdbcUtils.update(sql,cart.getShopnum(),cart.getUid(), cart.getGid());
		System.out.println("已修改" + row + "行数据");
	}

	// {4}根据用户ID查询,列出购物车
	public Cart queryById(Cart cart) throws SQLException {
		String sql = "select * from cart c where c.cartid=?";
		Cart c = JdbcUtils.query(sql, Cart.class, cart.getCartid());
		return c;
	}

	// {5}查询所有记录
	public List<Cart> queryAll() throws SQLException {
		List<Cart> list = null;
		String sql = "select * from cart c where c.cid <= ?";
		list = JdbcUtils.queryList(sql, Cart.class, 1000);
		return list;
	}
	
	// {6}查询一个用户所有记录
	//{6}根据用户ID查询收藏列表   g->i  u->g
	public List<Cart> querByuIdCart(int uid) throws SQLException{
		List<Cart> list = null;
		String sql = "select * from cart w where w.uid= ?";
		list = JdbcUtils.queryList(sql, Cart.class,uid);

		return list;
	}
	
	// {7}防止多条数据
	public Cart queryByUGId(Cart cart) throws SQLException {
		String sql = "select * from cart c where c.uid=? and c.gid=?";
		Cart c = JdbcUtils.query(sql, Cart.class, cart.getUid(),cart.getGid());
		return c;
	}
	
	//根据用户Id获得商品列表
	public List<Goods> querByuIdGoods(int uid) throws SQLException{
		List<Goods> list = null;
		String sql = "select goods.gid,gname,amount,info,oldprice,price,datatime,photo,grade,lable,sale from cart as pd join goods on pd.gid = goods.gid where pd.uid = ?";
		
		list = JdbcUtils.queryList(sql, Goods.class,uid);

		return list;
	}

}